package uk.ac.bath.gui;

import uk.ac.bath.environment.View;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import uk.ac.bath.environment.EnvironmentIF;
import uk.ac.bath.util.Tweakable;
import uk.ac.bath.util.TweakableInt;

public class ControlPanel extends JPanel {

    public ControlPanel(final View view, final EnvironmentIF env) {
        final TweakableInt frameRate = new TweakableInt(1, 40, 1, "Frame Rate");

        setLayout(new BorderLayout());


        // Frame Rate
        TweakerPanel p = new TweakerPanel();
        new SlideTweaker(p, frameRate);
        add(p, BorderLayout.NORTH);


        Observer obs = new Observer() {

            public void update(Observable o, Object arg) {
                long interval = 0;
                if (!frameRate.getMaximum().equals(frameRate.getNumber())) {
                    double xx = frameRate.doubleValue();
                    interval = (long) (1000 / Math.pow(1.2, xx));
                }
                env.setFrameRate(interval);
            }
        };

        frameRate.addObserver(obs);


        obs.update(frameRate, frameRate.getNumber());


        p = new TweakerPanel();

        Iterator iter = env.getTweaks().iterator();

        while (iter.hasNext()) {
            new SpinTweaker(p, (Tweakable) iter.next());
        }


        JButton killAll = new JButton("Zap");
        final JToggleButton dbgDisp = new JToggleButton("Dbg");
        killAll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                env.zap();
            }
        });


        dbgDisp.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                view.setDebug(dbgDisp.isSelected());
            }
        });

        add(killAll, BorderLayout.CENTER);
        add(dbgDisp, BorderLayout.WEST);

        add(p, BorderLayout.EAST);

    }

}
